<template>
    <view class="pub_page pub_page_bg pub_redPacket_log pub_header_padding">
        <pub-head headerName="redPacket_log" :headerConfig="headerConfig" @headerBack="headerBack"></pub-head>
        <pub-dialog v-show="dialogShow" dialogName="redPacket_log" :dialogConfig="dialogConfig"
            @dialogBack="dialogBack"></pub-dialog>
        <div class="pub_page_scroll page_box">
            <z-paging ref="Zpaging_log" class="lis_div" v-model="list" @query="getList" :refresher-enabled="false"
                :fixed="false">
                <div class="li" v-for="(li,key) in list" :key="key">
                    <div class="p p_1">
                        <div class="txt">红包单价：</div>
                        <div class="num">${{ openType==1?li.price:li.nowPrice }}</div>
                    </div>
                    <div class="p p_2">
                        <div class="txt">数量：</div>
                        <div class="num">{{ openType==1?li.totalNum:parseInt(li.integralNum-li.serviceNum) }}</div>
                    </div>
                    <div class="p p_3">
                        <div class="txt">总价：</div>
                        <div class="num">{{ openType==1?li.payNum:li.usdNum }}</div>
                    </div>
                    <div class="btn" v-if="openType==1 && li.status == 1" @click="toDeal('to_withdraw',li,key)">撤回</div>
                </div>
            </z-paging>
        </div>
    </view>
</template>

<script>
export default {
    data() {
        return {
            headerConfig: {
                title: '我发出的红包',//标题栏内容
                left_type: 3,
                right_type: 1,//右侧区域类型  0不出现  1文字  2图片
                right_content: '领取的红包',//右侧内容  right_type为1时文字内容 为2时图片链接
            },
            dialogShow: false,
            dialogType: '',
            dialogConfig: {
                title: '提示',//标题栏内容
                content: ''
            },
            packetId: '',
            packetKey: -1,

            openType: 1,//1我发出的红包 2我领取的红包

            list: []
        };
    },
    onLoad() {
        setTimeout(() => {
            this.$comFun.openLoading();
            this.$refs.Zpaging_log.reload()
        }, 400);
    },
    methods: {
        headerBack(data) {
            if (data.type == 'right') {
                this.openType = this.openType == 1 ? 2 : 1;
                this.headerConfig.title = this.openType == 1 ? '我发出的红包' : '我领取的红包';
                this.headerConfig.right_content = this.openType == 1 ? '领取的红包' : '发出的红包';
                this.list = [];
                this.$comFun.openLoading();
                this.$refs.Zpaging_log.reload();
            }
        },
        dialogBack(data) {
            // console.log(data);
            this.dialogShow = false;
            if (data.name == 'redPacket_log' && data.key == 1) {
                this.toDeal(this.dialogType)
            }
        },
        async getList(pageNo, pageSize) {
            let data = {
                pageNo: pageNo,
                pageSize: pageSize,
            };
            let url = ['', 'wall/getMySendPacketPage', 'wall/getMyReceivePacketPage']
            const res = await this.$request.get(url[this.openType], data);
            this.$comFun.closeLoading();
            // console.log(res);
            if (res.code == 200) {
                this.$refs.Zpaging_log.complete(res.result.records);
            } else {
                this.$refs.Zpaging_log.complete(false);
            }
        },
        async toDeal(type, data_1, data_2) {
            switch (type) {
                case 'to_withdraw':
                    this.packetId = data_1.id;
                    this.packetKey = data_2;
                    this.dialogType = 'sure_withdraw';
                    this.dialogConfig = {
                        title: '提示',//标题栏内容
                        content: '是否确定撤回？'
                    }
                    this.dialogShow = true;
                    break;
                case 'sure_withdraw':
                    this.$comFun.openLoading();
                    const res_2 = await this.$request.get('wall/withdraw', {
                        packetId: this.packetId,
                    });
                    // console.log(res_2);
                    this.$comFun.closeLoading();
                    if (res_2.code == 200) {
                        this.$comFun.toast('撤回成功');
                        this.list[this.packetKey].status = 3;
                        uni.$emit("redPacketReload")
                    } else {
                        this.$comFun.toast(res_2.message);
                    }
                    break;
                default:
                    break;
            }
        },
    },
};
</script>

<style lang="scss" scoped>
@import "@/common/css/redPacket.scss";
</style>
